import 'package:flutter/material.dart';
import './category_manage_page.dart';
import '../services/export_service.dart';
import './theme_settings_page.dart';

class SettingsPage extends StatelessWidget {
  final ThemeMode themeMode;
  final ValueChanged<ThemeMode> onThemeModeChanged;
  final Color primaryColor;
  final ValueChanged<Color> onPrimaryColorChanged;

  const SettingsPage({
    super.key,
    required this.themeMode,
    required this.onThemeModeChanged,
    required this.primaryColor,
    required this.onPrimaryColorChanged,
  });

  @override
  Widget build(BuildContext context) {
    return CustomScrollView(
      slivers: [
        const SliverAppBar(
          floating: true,
          title: Text('设置'),
        ),
        SliverList(
          delegate: SliverChildListDelegate([
            ListTile(
              leading: const Icon(Icons.category),
              title: const Text('分类管理'),
              onTap: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => const CategoryManagePage(),
                  ),
                );
              },
            ),
            ListTile(
              leading: const Icon(Icons.file_download),
              title: const Text('导出数据'),
              subtitle: const Text('导出为CSV文件'),
              onTap: () => _exportData(context),
            ),
            ListTile(
              leading: const Icon(Icons.color_lens),
              title: const Text('主题设置'),
              subtitle: Text(
                themeMode == ThemeMode.system
                    ? '跟随系统'
                    : themeMode == ThemeMode.light
                        ? '浅色'
                        : '深色',
              ),
              onTap: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => ThemeSettingsPage(
                      currentThemeMode: themeMode,
                      onThemeModeChanged: onThemeModeChanged,
                      currentPrimaryColor: primaryColor,
                      onPrimaryColorChanged: onPrimaryColorChanged,
                    ),
                  ),
                );
              },
            ),
          ]),
        ),
      ],
    );
  }

  Future<void> _exportData(BuildContext context) async {
    try {
      // 显示加载对话框
      showDialog(
        context: context,
        barrierDismissible: false,
        builder: (context) => const Center(
          child: CircularProgressIndicator(),
        ),
      );

      // 导出数据
      final filePath = await ExportService.exportToCSV();
      
      if (context.mounted) {
        // 关闭加载对话框
        Navigator.pop(context);
        
        // 显示成功对话框
        await showDialog(
          context: context,
          builder: (context) => AlertDialog(
            title: const Text('导出成功'),
            content: const Text('数据已成功导出为CSV文件，是否立即分享？'),
            actions: [
              TextButton(
                onPressed: () => Navigator.pop(context),
                child: const Text('关闭'),
              ),
              TextButton(
                onPressed: () {
                  Navigator.pop(context);
                  ExportService.shareCSV(filePath);
                },
                child: const Text('分享'),
              ),
            ],
          ),
        );
      }
    } catch (e) {
      if (context.mounted) {
        // 关闭加载对话框（如果存在）
        Navigator.maybeOf(context)?.pop();
        
        // 显示错误信息
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('导出失败: $e')),
        );
      }
    }
  }
} 